package com.atguigu.stack;

/**
 * @author shkstart
 * @create 2021-08-09 15:19
 */
public class Calculator {
    public static void main(String[] args) {
        //创建表达式
        String expression="300+2*6-2";
     //创建两个栈
        ArrayStack2 nunStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        //创建扫描
        int index=0;
        int num1=0;
        int num2=0;
        int oper=0;
        int res=0;
        String keepNum="";//用于拼接多位数
//        char ch = ' '; //将每次扫描得到char保存到ch
        //用于扫描归类，将数字与符号分开，进入栈中
        while (true){
            //获取数据
           char ch = expression.substring(index, index + 1).charAt(0);
            //判断是否是符号
            if (operStack.isOper(ch)){
                //如果是符号，判断该符号栈是否为空
                if (!operStack.isEmpty()){
                    //如果符号栈不为空,判断输入的符号等级与栈原本的符号等级相比
                    if (operStack.priority(ch)<=operStack.priority(operStack.peek())){
                        //如果获取的符号等级小于或者等于栈原本的符号等级，取出符号栈的栈顶和数字栈的两个数字
                        //进行计算
                         oper = operStack.pop();
                         num1=nunStack.pop();
                         num2=nunStack.pop();
                         res = nunStack.cal(num1, num2, oper);
                        nunStack.push(res);
                        operStack.push(ch);
                    }else {
                        //如果获取的符号等级大于栈原本的符号等级，直接把符号加入符号栈
                        operStack.push(ch);
                    }
                }else {
                    //如果符号栈为空
                    operStack.push(ch);
                }
            }else {
                //如果不是符号,而是数字
                //拼接多位数
                keepNum+=ch;
                //判断是否是最后一位数字
                if (index==expression.length()-1){
                    //如果是最后一位数字，那就直接入栈
                    nunStack.push(Integer.parseInt(keepNum));
                }else {
                    //如果不是，那就判断下一位是否是数字
                    if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))){
                        //如果是符号，那直接入栈
                        nunStack.push(Integer.parseInt(keepNum));
                        //清空keepNUM
                        keepNum="";
                    }
                }

            }
            //推进扫描，判断结束
            index++;
            if (index>=expression.length()){
                break;
            }
        }

        while(true) {
            //如果符号栈为空，则计算到最后的结果, 数栈中只有一个数字【结果】
            if(operStack.isEmpty()) {
                break;
            }
            num1 = nunStack.pop();
            num2 = nunStack.pop();
            oper = operStack.pop();
            res = nunStack.cal(num1, num2, oper);
            nunStack.push(res);//入栈
        }
        //将数栈的最后数，pop出，就是结果
        int res2 = nunStack.pop();
        System.out.printf("表达式 %s = %d", expression, res2);
    }
}
//创建一个栈
class ArrayStack2{
    private int maxSize;
    private int [] stack;
    private int top=-1;
    //创建一个构造器
    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //判断栈满
    public boolean isFull(){
        return top==maxSize-1;
    }
    //判断栈空
    public boolean isEmpty(){
        return top==-1;
    }
    //入栈
    public  void push(int value){
        if (isFull()){
            System.out.println("栈满");
            return;
        }
        top++;
        stack[top]=value;
    }
    //出栈
    public int pop(){
        if (isEmpty()){
//            System.out.println("栈空");
//            return 0;
            throw new RuntimeException("栈kong，没有数据");
        }
        int value=stack[top];
        top--;
        return value;
    }
    //遍历栈
    public void list(){
        if (isEmpty()){
            System.out.println("栈空，没有数据");
            return;
        }
        for (int i=top;i>=0;i--){
            System.out.println("stack["+i+"]="+stack[i]);
        }
    }
    //判断运算符的优先权
    public int priority(int oper){
        if (oper=='*'||oper=='/'){
            return 1;
        }else if (oper=='+'||oper=='-'){
            return 0;
        }else {
            return -1;
        }
    }
    //判断是否为符号
    public boolean isOper(char val){
        return val=='+'||val=='-'||val=='*'||val=='/';
    }
    //计算方法
    public int cal(int num1,int num2,int oper){
        int res=0;
        switch (oper){
            case '+':
                res=num1+num2;
                break;
            case '-':
                //注意顺序
                res=num2-num1;
                break;
            case '*':
                res=num1*num2;
                break;
            case '/':
                res=num2/num1;
                break;
                default:
                    break;
        }
        return res;
    }
    //返回栈顶的值，但是不取出对应的数值
    public int peek(){
        return stack[top];
    }

}
